from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text, ForeignKey, BigInteger
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..core.database import Base

class FileVersion(Base):
    __tablename__ = "file_versions"
    
    id = Column(Integer, primary_key=True, index=True)
    file_id = Column(Integer, ForeignKey("files.id"), nullable=False)
    version_number = Column(Integer, nullable=False)  # 版本号，从1开始
    path = Column(String(500), nullable=False)  # 版本文件的物理路径
    size = Column(BigInteger, default=0)  # 文件大小（字节）
    mime_type = Column(String(100), nullable=True)
    checksum = Column(String(64), nullable=True)  # 文件校验和
    comment = Column(Text, default="")  # 版本说明
    created_by = Column(Integer, ForeignKey("users.id"), nullable=False)
    is_current = Column(Boolean, default=False)  # 是否为当前版本
    is_deleted = Column(Boolean, default=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    file = relationship("File", back_populates="versions")
    creator = relationship("User", foreign_keys=[created_by])
    
    def __repr__(self):
        return f"<FileVersion(id={self.id}, file_id={self.file_id}, version={self.version_number})>"